<template>
	<view>
		<!-- <scroll-view scroll-x class="bg-white nav fixed" v-if="tabList.length!=1">
			<view class="flex text-center">
				<view class="cu-item flex-sub font-45" :class="index==TabCur?'text-myred my-cur':''"
					v-for="(item,index) in tabList" :key="index" @tap="tabSelect(index)">
					{{ tabList[index].title }}
				</view>
			</view>
		</scroll-view> -->
		<!-- 备品备件 -->
		<view class="grid-wrap" :class="tabIndex==0?'grid-wrap-first':'' "
			v-for="(tabItem, tabIndex) in tabListConfig[TabCur]" :key="tabIndex">
			<view class="business-type-name">
				{{ tabItem.type }}
			</view>
			<view class="business-menu flex">
				<view class="business-menu-item" v-for="(item,index) in tabItem.children" @click="handelMenu(item)"
					:key="index" v-if="userInfo.authModel.indexOf(item.auth) == -1 ?false:true  ">
					<view class="business-menu-name">
						{{ item.name }}
					</view>
					<image :src="item.icon" mode=""></image>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import api from '@/common/api.js'
	const plug = uni.requireNativePlugin('Html5app-Gprinter');
	let _self;

	let allMenu = {
		0: [{
				type: '入库业务',
				children: [{
						name: '新增入库申请',
						path: './mater_list',
						icon: '/static/menu/ico_cc_sqrk.png',
						sqlTbName: 'applyIn',
						auth: 'R1001'
					}, {
						name: '申请单入库',
						path: './inbound/list',
						icon: '/static/menu/ico_cc_sqd.png',
						sqlTbName: 'ST_INAPPLY',
						auth: 'R1002'
					},
					{
						name: '供货清单入库',
						path: './inbound/list',
						icon: '/static/menu/ico_cc_ghqd.png',
						sqlTbName: 'PP_SUPLIST',
						auth: 'R1003'
					},
					{
						name: '采购订单入库',
						path: './inbound/list',
						icon: '/static/menu/ico_cc_cgdd.png',
						sqlTbName: 'PP_PURORDERS',
						auth: 'R1004'
					},
					{
						name: '手工单入库',
						path: './mater_list',
						icon: '/static/menu/ico_cc_sgd.png',
						sqlTbName: 'manualIn',
						auth: 'R1005'
					},

					{
						name: '收货确认',
						path: './inbound/list-ghd',
						icon: '/static/menu/ico_cc_sgd.png',
						sqlTbName: 'PP_SUPLIST',
						auth: 'R1005'
					},

					{
						name: '入库单查询',
						path: './inbound/over-list',
						icon: '/static/menu/ico_cc_rkdcx.png',
						sqlTbName: 'ST_PURINSTORE',
						auth: 'R1006'
					},
				]
			},
			{
				type: '出库业务',
				children: [{
						name: '新增出库申请',
						path: './mater_list',
						icon: '/static/menu/ico_cc_sqd.png',
						sqlTbName: 'applyOut',
						auth: 'C1001'
					},
					{
						name: '申请单出库',
						path: './inbound/list',
						icon: '/static/menu/ico_cc_sqck.png',
						sqlTbName: 'ST_OUTAPPLY',
						auth: 'C1002'
					},

					{
						name: '入库单出库',
						path: './outbound/in-over-list',
						icon: '/static/menu/ico_cc_sqck.png',
						sqlTbName: 'ST_OUTAPPLY_OUT',
						auth: 'C1002' // C1008
					},

					{
						name: '手工单出库',
						path: './mater_list',
						icon: '/static/menu/ico_cc_sgd.png',
						sqlTbName: 'manualOut',
						auth: 'C1003'
					},
					{
						name: '出库红冲',
						path: './outbound/over-list',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: 'ST_OUTSTOCK',
						auth: 'C1004'
					},

					{
						name: '打印库存物资',
						path: './outbound/stock_mater_print',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: 'ST_OUTSTOCK',
						auth: 'C1007'
					},
					{
						name: '库存量查询',
						path: './outbound/Inventory-amount',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: 'ST_OUTSTOCK',
						auth: 'C1007'
					},
				]
			},
			{
				type: '盘点业务',
				children: [{
						name: '盘点',
						path: './inventory/new_submit',
						icon: '/static/menu/ico_cc_pddlr.png',
						sqlTbName: 'manual',
						auth: 'P1001'
					},
					{
						name: '继续盘点',
						path: './inventory/list-order',
						icon: '/static/menu/ico_cc_pdcx.png',
						sqlTbName: 'apply',
						auth: 'P1002'
					},

					{
						name: '临时盘点',
						path: './inventory/new_submit_ls',
						icon: '/static/menu/ico_cc_pdcx.png',
						sqlTbName: 'apply',
						auth: 'P1002'
					},

					{
						name: '自动盘点',
						path: './inventory/video-submit',
						icon: '/static/menu/ico_cc_pdcx.png',
						sqlTbName: 'apply',
						auth: 'P1002'
					},

					{
						name: '盘点统计',
						path: '/pages/public/ztree-pdtj',
						icon: '/static/menu/ico_cc_pdrl.png',
						sqlTbName: 'ST_OUTAPPLY',
						auth: 'P1003'
					}
				]
			},
			{
				type: '工器具管理',
				children: [{
						name: '工具办理申请',
						path: './outbound/mater-list',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: 'ST_OUTSTOCK',
						auth: 'C1005'
					},
					{
						name: '工具借出',
						path: './outbound/out-mater-list',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: 'ST_OUTSTOCK',
						auth: 'C1005'
					},

					{
						name: '工具归还',
						path: './outbound/out-mater-list',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: 'ST_OUTSTOCK',
						auth: 'C1006'
					},
				]
			}
		],
		1: [{
				type: '入库业务',
				children: [{
						name: '交货单入库',
						path: './ton-pack/saleorder',
						icon: '/static/menu/ico_cc_jhdrk.png',
						sqlTbName: 'DB_INSTOCK',
						auth: 'R1001'
					},
					{
						name: '入库单查询',
						path: './ton-pack/over-list',
						icon: '/static/menu/ico_cc_rkdcx.png',
						sqlTbName: '0',
						auth: 'R1001'
					},
				]
			},
			{
				type: '出库业务',
				children: [{
						name: '入库单出库',
						path: './ton-pack/over-list',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: '2',
						auth: 'R1001'
					},
					{
						name: '出库单查询',
						path: './ton-pack/over-list',
						icon: '/static/menu/ico_cc_ckdcx.png',
						sqlTbName: '1',
						auth: 'R1001'
					},
				]
			},
			{
				type: '质保书',
				children: [{
					name: '质保书查询',
					path: './ton-pack/weight',
					icon: '/static/menu/ico_cc_smcx.png',
					sqlTbName: '1',
					auth: 'R1001'
				}]
			},
		],
	}


	export default {
		computed: mapState(['userInfo']),
		data() {
			return {
				tabList: [{
						title: '备品备件',
					},
					{
						title: '外销水泥',
					}
				],
				TabCur: 0,
				scrollLeft: 0,
				tabListConfig: allMenu
			};
		},
		onLoad() {
			_self = this
			this.getDictionarySL()
			this.getNum()
			this.autoConnectPrint()
			this.initAuth()
		},
		methods: {
			...mapMutations(['setTaxRate', 'setApprovalNum']),

			// 获取待审批数量
			getNum() {
				let query = {
					"page": 1,
					"rows": "1",
					"sidx": "",
					"sord": "",
					"queryJson": "{\"ISFINISH\":\"0\"}",
					"userId": this.userInfo.UserId,
					"account": this.userInfo.Account
				}
				let _self = this;
				let URL = this.userInfo.BaseURL + api.apiPublic.approvalList;
				this.axios({
					url: URL,
					data: query
				}).then(res => {
					let {
						IsSuccess,
						Result
					} = res;
					// console.log(Result);
					if (!IsSuccess) return null;
					let approvalNum = Result.approvalnum || 0;
					let approvedNum = Result.approvednum1 || 0;
					let rejectNum = Result.approvednum2 || 0;
					_self.setApprovalNum({
						approvalNum,
						approvedNum,
						rejectNum
					})
					uni.setTabBarBadge({
						index: 2,
						text: approvalNum.toString()
					})
				})
			},

			getDictionarySL() {
				let URL = '/api/APPCommon/GetDictionaryList';
				let queryData = {
					"page": 1,
					"rows": "500",
					"sidx": "",
					"sord": "",
					"queryJson": "{\"ITEMCODE\":\"\"}",
					"account": _self.userInfo.Account
				};
				// console.log(789,queryData);
				//  MaterialUse用途字典   SL税率字典
				this.axios({
					url: URL,
					data: queryData,
					isLoading: 1
				}).then(res => {
					let {
						IsSuccess,
						Result,
						Message
					} = res;
					if (!IsSuccess) {
						return _self.$utils.tips('税率字典数据请求失败')
					}
					let taxRate = [{
							ItemName: '--请选择--',
							ItemValue: ''
						}],
						wareType = [],
						outType = [],
						materUse = [{
							ItemName: '--请选择--',
							ItemValue: ''
						}];
					let ary = Result.forEach(item => {
						item.ItemCode == 'SL' ? taxRate.push(item) : null
						item.ItemCode == 'MaterialUse' ? materUse.push(item) : null
						item.ItemCode == 'RKLX' ? wareType.push(item) : null
						item.ItemCode == 'CKLX' ? outType.push(item) : null
					})
					// console.log(taxRate);
					_self.setTaxRate({
						taxRate: taxRate,
						materUse: materUse,
						wareType: wareType,
						outType: outType
					})
				})
			},

			tabSelect(e) {
				this.TabCur = e;
				this.scrollLeft = (e - 1) * 60;
			},

			handelMenu(menu) {
				// console.log(menu);
				if (menu.sqlTbName == 'ScanIn' || menu.sqlTbName == 'ScanOut') {
					return _self.openScan();
				}
				let path = menu.path + '?pageTitle=' + menu.name + '&sqlTbName=' + menu.sqlTbName + '&auth=' + menu.auth;
				// console.log(path);
				uni.navigateTo({
					url: path
				})
			},

			initAuth() {

			},

			openScan(id) {
				// _isActiveStore = true ;
				// console.log(id,index)
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						console.log(res)
						if (res.scanType != 'QR_CODE') {
							return uni.showToast({
								icon: 'none',
								title: '类型错误，请重新扫描'
							})
						}

						let path = '/pages/tab01/d-inbound/detail_scan?t=' + res.result;
						console.log(path);
						uni.navigateTo({
							url: path
						})
					}
				});

			},

			// 启动蓝牙 并连接蓝牙打印机
			autoConnectPrint() {
				uni.getStorage({
					key: 'printMac',
					success(res) {
						console.log(res);
						_self.connectPrint(res.data)
					}
				})
			},

			connectPrint(mac) {
				//连接打印机
				plug.connectDevices({
					address: mac
				}, ret => {
					// console.log(JSON.stringify(ret));
					if (ret.state == 1 || ret.state == 4) {
						_self.OpenPrintSet()
					}
				});
			},

			OpenPrintSet() {
				uni.showModal({
					title: '提示',
					content: '蓝牙自动连接失败,是否重新配置打印机?',
					success(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/tab05/print_set'
							})
						}
					}
				})
			}




		}
	}
</script>

<style lang="scss">
	.my-cur {
		font-weight: 600;
	}

	.grid-wrap-first {
		// margin-top: 110rpx;
	}

	.grid-wrap {
		width: 750rpx;
		padding: 20rpx;

		.business-type-name {
			font-size: 45rpx;
			font-weight: 600;
		}

		.business-menu {
			flex-wrap: wrap;

			.business-menu-item {
				width: 340rpx;
				height: 140rpx;
				border-radius: 14rpx;
				display: flex;
				align-items: center;
				background-color: #FFFFFF;
				margin-right: 26rpx;
				margin-top: 18rpx;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-right: 10rpx;
				}

				.business-menu-name {
					width: 230rpx;
					padding-left: 20rpx;
					font-size: 34rpx !important;
					font-weight: 600;
					color: #444;
				}
			}

			.business-menu-item:nth-child(2n) {
				margin-right: 0;
				// margin-left: 5rpx;
			}
		}
	}
</style>